﻿using System;
using System.Collections.Generic;
using System.Deployment.Application;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

using TraceView.Shared;

namespace TraceView.UI.Entities
{
    [DataContract(IsReference = true)]
    internal sealed class Settings
    {
        private Settings()
        {
        }

        private static Settings _instance;

        internal static Settings Instance
        {
            get { return _instance ?? (_instance = new Settings()); }
        }

        [DataMember]
        internal int Level = 2;

        [DataMember]
        internal int MaxDuration = 10;

        [DataMember]
        internal int MaxItems = 10;

        [DataMember]
        internal string SearchString = string.Empty;

        [DataMember(Name = "SeverityFilters")]
        private List<Severity> _severityFilters = new List<Severity>();

        internal IList<Severity> SeverityFilters
        {
            get { return _severityFilters; }
        }

        [DataMember]
        internal bool ShowNotifications = true;

        [DataMember]
        internal string ThemeColor = "Default";

        internal static void Load()
        {
            string settingsFilePath = GetSettingsFilePath();
            if (!File.Exists(settingsFilePath))
                return;

            FileStream settingsFile = File.OpenRead(settingsFilePath);
            var deserializer = new DataContractSerializer(typeof(Settings));

#if DEBUG
            _instance = deserializer.ReadObject(settingsFile) as Settings;
#else
            using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(settingsFile, XmlDictionaryReaderQuotas.Max))
                _instance = deserializer.ReadObject(reader) as Settings;
#endif
        }

        internal static void Save()
        {
            string settingsFilePath = GetSettingsFilePath();
            using (FileStream settingsFile = File.Create(settingsFilePath))
            {
                var serializer = new DataContractSerializer(typeof(Settings));

#if DEBUG
                serializer.WriteObject(settingsFile, Instance);
                settingsFile.Flush(true);
#else
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(settingsFile))
                    serializer.WriteObject(writer, Instance);
#endif
            }
        }

        private static string GetSettingsFilePath()
        {
            string dataDirectory;
            if (ApplicationDeployment.IsNetworkDeployed)
                dataDirectory = ApplicationDeployment.CurrentDeployment.DataDirectory;
            else
            {
                dataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    "TraceView");
                if (!Directory.Exists(dataDirectory))
                    Directory.CreateDirectory(dataDirectory);
            }

            string settingsFilePath = Path.Combine(dataDirectory, SettingsFileName);
            return settingsFilePath;
        }

#if DEBUG
        private const string SettingsFileName = "Settings.xml";
#else
        private const string SettingsFileName = "Settings.bin";
#endif
    }
}